package com.genITeam.ria.bl;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.genITeam.ria.constant.Constants;
import com.genITeam.ria.dao.LoginDAO;
import com.genITeam.ria.exception.NGFException;
import com.genITeam.ria.utility.HibernateUtils;
import com.genITeam.ria.vo.Member;

public class LoginBL {

	public LoginBL() {
		BasicConfigurator.configure();
	}

	Logger logger = Logger.getLogger(LoginBL.class);


	/**
	 * verifyLogin Verify the Login information against provided usernaem and password
	 * 
	 * @param Member
	 * 
	 * @return String
	 * @throws NGFException
	 */
	
	
	public String verifyLogin(Member member) throws NGFException {
		String xmlVerifyLogin = "";
		String status = "";
		Session session = null;
		Transaction tx = null;
		try {
			logger.info("Start verifyLogin");
			System.out.println("verifyLogin BL");
			LoginDAO loginDAO = new LoginDAO();
			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			
			Member memberVo = loginDAO.verifyUser(member, session);	

			
			if(memberVo == null){
				status = Constants.FALSE;
				xmlVerifyLogin = this.generateLoginFailxml(member, status);
			}else{
				status = Constants.TRUE;				
				xmlVerifyLogin = this.generateLoginxml(memberVo, status);
			}
			
			tx.commit();

		} catch (Exception e) {
			System.out.println(e.toString());
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} // end of if
			logger.error("Exception = "+e.toString());
			throw new NGFException(e.getMessage());

		}

		return xmlVerifyLogin;
	}

	

	/**
	 * generateLoginxml return the login inforamtion in XML format for user validation
	 * 
	 * @param Member, String
	 * 
	 * @return String
	 * @throws NGFException
	 */
	
	
	public String generateLoginxml(Member member, String status ) throws NGFException {

		StringBuffer xml = new StringBuffer("");
		try {
			logger.info("Start generateLoginxml");
			String role = "";
			if( member.getRole() == 'N'){
				role = Constants.NORMAL;
			}else if( member.getRole() == 'A'){
				role = Constants.ADMINISTRATOR;
			}else if( member.getRole() == 'M'){
				role = Constants.MODERATOR;
			}
			
			xml.append("<login>");
			xml.append("<id>" + member.getId() + "</id>");				
			xml.append("<userName>" + member.getUserName() + "</userName>");
			xml.append("<password>" + member.getPassword() + "</password>");
			xml.append("<loggedIn>" + status + "</loggedIn>");	
			xml.append("<role>" + role + "</role>");						
			xml.append("</login>");
			xml.append("\n");

			System.out.println(xml);

		} catch (Exception e) {
			logger.error("Exception = "+e.toString());
			System.out.println(e.toString());
			throw new NGFException(e.getMessage());
		}

		return xml.toString();
	}
	
	public String generateLoginFailxml(Member member, String status ) throws NGFException {

		StringBuffer xml = new StringBuffer("");
		try {
			logger.info("Start generateLoginFailxml");

			
			xml.append("<login>");
			xml.append("<id>0</id>");				
			xml.append("<userName>" + member.getUserName() + "</userName>");
			xml.append("<password>" + member.getPassword() + "</password>");
			xml.append("<loggedIn>" + status + "</loggedIn>");	
			xml.append("<role> </role>");						
			xml.append("</login>");
			xml.append("\n");

			System.out.println(xml);

		} catch (Exception e) {
			logger.error("Exception = "+e.toString());
			System.out.println(e.toString());
			throw new NGFException(e.getMessage());
		}

		return xml.toString();
	}	
}
